package com.example.config;

import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.example.mapper.LotteryTicketMapper;
import com.example.model.LotteryTicket;
import com.example.service.LotteryTicketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class StartConfig {
    @Autowired
    private LotteryTicketService lotteryTicketService;
    @Autowired
    private LotteryTicketMapper LotteryTicketMapper;

    //@PostConstruct
    /*public void syncNewData(){
        List<LotteryTicket> list = LotteryTicketMapper.selectTicketListLimit("desc", 1);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);    //获取年
        // 没有数据，则加载数据
        if(CollectionUtils.isEmpty(list)){
            log.info("############################ 第一次 同步数据开始 ############################");
            for(int i = 1976; i <= year; i++){
                lotteryTicketService.batchInsertLotteryTicket(i);
            }
            try{
                lotteryTicketService.calculateRepeat("desc");
            }catch (Exception e){
                log.info("StartConfig calculateRepeat:", e);
            }
            return;
        }
        int month = calendar.get(Calendar.MONTH)+1;    //获取月
        int date = calendar.get(Calendar.DATE);    //获取日
        String currentDate = year +"-"+(month<10? "0"+month:month+"") +"-"+ (date<10? "0"+date:date+"");
        String publishDate = list.get(0).getPublishDate(); // 期数
        Date startDate=DateUtil.parse(publishDate);
        Date endDate=DateUtil.parse(currentDate);
        //相差毫秒数
        long millis=DateUtil.between(startDate,endDate, DateUnit.MS);
        //相差1天(天)
        String formatDay = DateUtil.formatBetween(millis, BetweenFormatter.Level.DAY);
        int count = Integer.parseInt(formatDay.substring(0,1));

        count = count / 2;  //更新最新多少期
        if(count == 0){
            log.info("############################ 暂时不用更新数据 ############################");
            return;
        }
        log.info("############################ 更新开始 ############################");
        lotteryTicketService.batchInsertLotteryTicket(count);
        try{
            lotteryTicketService.calculateNewDataRepeat("desc", count+7);
        }catch (Exception e){
            log.info("StartConfig calculateRepeat:", e);
        }
    }*/

   // public static void main(String[] args) {
        /*Date startDate=DateUtil.parse("2021-04-22");
        Date endDate=DateUtil.parse("2021-04-21");
        //相差毫秒数
        long millis=DateUtil.between(startDate,endDate, DateUnit.MS);
        //相差1天(天)
        String formatDay = DateUtil.formatBetween(millis, BetweenFormatter.Level.DAY);
        System.out.println(formatDay.substring(0,1));

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);    //获取年
        int month = calendar.get(Calendar.MONTH)+1;    //获取月
        int date = calendar.get(Calendar.DATE);    //获取日
        String currentDate = year +"-"+(month<10? "0"+month:month+"") +"-"+ (date<10? "0"+date:date+"");
        System.out.println(currentDate);*/
        /*int countt = 7;
        countt = countt /2;
        System.out.println(countt);*/
    //}
}
